package com.ExcelServer.service.impl;


import com.ExcelServer.common.Result;
import com.ExcelServer.mapper.ExcelRulesMapper;
import com.ExcelServer.pojo.entity.ExcelCreateInfo;
import com.ExcelServer.pojo.entity.ExcelRules;
import com.ExcelServer.pojo.form.update.UpdateExcelRulesForm;
import com.ExcelServer.pojo.query.ExcelRulersQuery;
import com.ExcelServer.service.ExcelRulesService;
import com.ExcelServer.util.EquationVerification;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author admin
* @description 针对表【excel_rules】的数据库操作Service实现
* @createDate 2024-01-07 20:56:38
*/
@Service
public class ExcelRulesServiceImpl extends ServiceImpl<ExcelRulesMapper, ExcelRules>
implements ExcelRulesService {

    @Autowired
    ExcelRulesMapper excelRulesMapper;

    @Autowired
    ExcelCreateInfoServiceImpl excelCreateInfoService;

    @Autowired
    EquationVerification equationVerification;

    public Result getExcelRule(ExcelRulersQuery excelRulersQuery) {
       List<ExcelRules> excelRulesList= excelRulesMapper.getExcelRule(excelRulersQuery);
        return Result.success(excelRulesList);
    }

    @Override
    public Result updateRuler(UpdateExcelRulesForm updateExcelRulesForm) {

        //step 1 校验公式是否存在非法字段
        Integer tableId = updateExcelRulesForm.getTableId();

        ExcelCreateInfo excelCreateInfo = excelCreateInfoService.getById(tableId);

        //step 2 校验公式是否能通过自检查和数学检验
        if(excelCreateInfo!=null){
            //自校验 addOneExcelRulerForm.getTableRule()是否合法;
            String tableRule = updateExcelRulesForm.getTableRule();

            //校验表中是否存在等式的所有的操作表示数 ，缺一个都不合法，直接返回。
            Object property = excelCreateInfo.getProperty();
            List<String> keys = new ArrayList<>();
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode rootNode = objectMapper.readTree( String.valueOf(property));

                Iterator<String> fieldNames = rootNode.fieldNames();

                while (fieldNames.hasNext()) {
                    String key = fieldNames.next();
                    keys.add(key);
                }
                System.out.println(keys);
            }catch (Exception e){
                e.printStackTrace();
            }

            //正则表达式获取tableRule 中的操作数。
            List<String> operands = new ArrayList<>();
            Pattern pattern = Pattern.compile("_\\d+");
            Matcher matcher = pattern.matcher(tableRule);
            while (matcher.find()) {
                String operand = matcher.group();
                operands.add(operand);
            }

            //检验参数是否合法
            for (String operand : operands) {
                if (keys.contains(operand)) {
                    continue;
                } else {
                    return Result.fail( "公式不正确,存在非法参数");
                }
            }



            // 数学校验等式是否合法，不合法也返回
            boolean verification = equationVerification.verification(tableRule);

            if(verification){
                excelRulesMapper.updateById(updateExcelRulesForm);
                return Result.success("公式修改成功");
            }else {
                System.out.println("verification"+verification);
                return Result.fail("公式修改失败，规则不正确");
            }

        }

        return Result.fail("发生未知错误");
    }
}
